// eee.Sheffield.PZ.Imaging
//
// Copyright ?Ping Zou, 2007
// sg71.cherub@gmail.com

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace eee.Sheffield.PZ.Imaging
{
    /// <summary>
    /// center point field C
    /// </summary>
    public class CenterPointField
    {
        public int count = 0;
        public double mean_r = 0;
        public double mean_mu = 0;
        public double mean_B = 0;

        public PZPoint mStartCentrePoint = new PZPoint();
        public PZDirection mStartVesselDirection = new PZDirection();
        public PZPoint mEndCentrePoint = new PZPoint();
        public PZDirection mEndVesselDirection = new PZDirection();

        public List<PZPoint> cpArray = new List<PZPoint>();
        public List<double> rArray = new List<double>();
        public List<double> muArray = new List<double>();
        public List<double> BArray = new List<double>();
        public List<PZDirection> vdArray = new List<PZDirection>();

        public CenterPointField()
        {
        }

        /// <summary>
        /// initialize center point field from track result
        /// </summary>
        /// <param name="tr"></param>
        public void InitFromTrackResult(TrackResult tr)
        {
            count = tr._count;
            for (int i = 0; i < tr._count; i++)
            {
                PZPoint tempCP = new PZPoint();
                PZDirection tempVD = new PZDirection();
                tempCP.MemCopyFrom(tr._cpArray[i]);
                tempVD.MemCopyFrom(tr._vdArray[i]);
                cpArray.Add(tempCP);
                muArray.Add(tr._muArray[i]);
                BArray.Add(tr._BArray[i]);

                double tempr = tr._rArray[i];
                double theta = System.Math.Atan(tempVD.x / tempVD.y);
                double rh = tempr * System.Math.Cos(theta);
                double rv = tempr * System.Math.Sin(theta);
                rArray.Add(rh > rv ? rh : rv);
                vdArray.Add(System.Math.Abs(tempVD.x) > System.Math.Abs(tempVD.y) ?
                    new PZDirection(System.Math.Sign(tempVD.x) * 1, 0) :
                    new PZDirection(0, System.Math.Sign(tempVD.y) * 1));
            }
        }

        /// <summary>
        /// write to file
        /// </summary>
        /// <param name="filename"></param>
        public void Write(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
            StreamWriter w = new StreamWriter(fs);
            /*
            // start centre point and direction
            w.WriteLine(mStartCentrePoint.ToString() + " " + mStartVesselDirection.ToString());
            for (int i = 0; i < cpArray.Count; i++)
            {
                w.Write(String.Format("{0, -5} ", (i + 1)));
                w.Write(String.Format("{0, -7:0.00} ", ((PZPoint)cpArray[i]).x));
                w.Write(String.Format("{0, -7:0.00} ", ((PZPoint)cpArray[i]).y));
                w.Write(String.Format("{0, -7:0.00} ", ((PZDirection)vdArray[i]).x));
                w.Write(String.Format("{0, -7:0.00} ", ((PZDirection)vdArray[i]).y));
                w.Write(String.Format("{0, -7:0.00} ", ((PZPoint)ep1Array[i]).x));
                w.Write(String.Format("{0, -7:0.00} ", ((PZPoint)ep1Array[i]).y));
                w.Write(String.Format("{0, -7:0.00} ", ((PZPoint)ep2Array[i]).x));
                w.Write(String.Format("{0, -7:0.00} ", ((PZPoint)ep2Array[i]).y));
                w.Write(String.Format("{0, -7:0.000} ", (double)rArray[i]));
                w.Write(String.Format("{0, -7:0.000} ", (double)muArray[i]));
                w.WriteLine(String.Format("{0, -7:0.00}", (double)BArray[i]));
            }
            */
            w.Close();
            fs.Close();
        } // Write()

        /// <summary>
        /// read from file
        /// </summary>
        /// <param name="filename"></param>
        public void Read(string filename)
        {
            // clear ArrayList;
            cpArray.Clear();
            rArray.Clear();
            muArray.Clear();
            BArray.Clear();
            vdArray.Clear();

            // read in a delimited file
            //string[] fields;
            string[] firstLineDelimiter = new string[] { " ", ",", "(", ")" };
            string[] delimiter = new string[] { " " };
            string[] trackResult = new string[12];
            //bool firstLine = true;
            /*
            using (Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(filename))
            {
                parser.Delimiters = delimiter;
                while (!parser.EndOfData)
                {
                    if (firstLine)
                    // first line
                    {
                        parser.Delimiters = firstLineDelimiter;
                        fields = parser.ReadFields();
                        int j = 0;
                        for (int i = 0; i < fields.Length; i++)
                        {
                            if (fields[i] == "")
                                continue;
                            else
                            {
                                trackResult[j] = fields[i];
                                j++;
                            }
                        }
                        double cpx = Convert.ToDouble(trackResult[0]);
                        double cpy = Convert.ToDouble(trackResult[1]);
                        double vdx = Convert.ToDouble(trackResult[2]);
                        double vdy = Convert.ToDouble(trackResult[3]);
                        mStartCentrePoint = new PZPoint(cpx, cpy);
                        mStartVesselDirection = new PZDirection(vdx, vdy);
                        firstLine = false;
                    }
                    else
                    // rest of lines
                    {
                        parser.Delimiters = delimiter;
                        //Read in the fields for the current line
                        fields = parser.ReadFields();
                        int j = 0;
                        for (int i = 0; i < fields.Length; i++)
                        {
                            if (fields[i] == "")
                                continue;
                            else
                            {
                                trackResult[j] = fields[i];
                                j++;
                            }
                        }
                        // read to arrays
                        double cpx = Convert.ToDouble(trackResult[1]);
                        double cpy = Convert.ToDouble(trackResult[2]);
                        double vdx = Convert.ToDouble(trackResult[3]);
                        double vdy = Convert.ToDouble(trackResult[4]);
                        double ep1x = Convert.ToDouble(trackResult[5]);
                        double ep1y = Convert.ToDouble(trackResult[6]);
                        double ep2x = Convert.ToDouble(trackResult[7]);
                        double ep2y = Convert.ToDouble(trackResult[8]);
                        double inputr = Convert.ToDouble(trackResult[9]);
                        double inputmu = Convert.ToDouble(trackResult[10]);
                        double inputB = Convert.ToDouble(trackResult[11]);
                        PZPoint cp = new PZPoint(cpx, cpy);
                        PZPoint ep1 = new PZPoint(ep1x, ep1y);
                        PZPoint ep2 = new PZPoint(ep2x, ep2y);
                        PZDirection vd = new PZDirection(vdx, vdy);
                        cpArray.Add(cp);
                        ep1Array.Add(ep1);
                        ep2Array.Add(ep2);
                        vdArray.Add(vd);
                        rArray.Add(inputr);
                        muArray.Add(inputmu);
                        BArray.Add(inputB);
                    }
                }
            }*/
        } // Read()
    }
}
